<?php

class Admin_NewsController extends ZF_AdminController{
    
	public function indexAction(){
            $model = new Admin_Model_News();
            $paginator = Zend_Paginator::factory($model->listall());
            $paginator->setItemCountPerPage(20);
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->news = $paginator;
            $this->view->title = "NEWS MANAGEMENT";
            
            
	}
        public function addAction(){
            $form = new Admin_Form_AddNewsForm();
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($_POST)) {
                   
                    $dateFormat = "Y-m-d H:i:s";
                    $model = new Admin_Model_News();
                    
                    $result = $model->insert(
                                    $form->getValue("title"),
                                    $form->getValue("thumbnail_url"),date($dateFormat),
                                    $form->getValue("title_en"),
                                    $form->getValue("content"),  $form->getValue("content_en"),
                                    $form->getValue("des"),$form->getValue("des_en"),
                                    1,0,0);
                    echo $result;
                    if ($result>0) {
                            
                            $id_news = $model->getLastId();
                            $data_cats = $this->getRequest ()->getPost( 'cats_item', null );
                            $model_news_cat = new Admin_Model_NewsCategory();
                            if(isset($data_cats)){
                                foreach ($data_cats as $item){
                                    
                                    $model_news_cat->insert($id_news, $item);
                                }
                            }
                            $this->_redirect("/".$this->module."/news/index");
                    }                    
                }
            } else {
                
                $form->setAction($this->baseUrl."/".$this->module."/news/add");
                $this->view->form = $form;
                $this->view->title = "ADD NEW NEWS";
                $this->view->group = "NEWS";
                $this->view->group_action = "ADD";
                

            }
        }
        public function editAction(){
            $id = $this->_request->getParam('id');
            //$form = new Admin_Form_EditNewsForm();
            $model = new Admin_Model_News();
            if ($this->getRequest()->isPost()) {
               {
                    $dateFormat = "Y-m-d H:i:s";
                    $model->getById($this->getRequest()->getPost("id"));
                    $model->setTitle($this->getRequest()->getPost("title"));
                    $model->setTimestamp(date($dateFormat));
                    
                    $model->setThumbnailUrl($this->getRequest()->getPost("thumbnail_url"));
                    $model->setContent($this->getRequest()->getPost("content"));                                  
                    $model->setDes($this->getRequest()->getPost("des"));
                    $model->setDes_En($this->getRequest()->getPost("des_en"));
                    $model->setContent_En($this->getRequest()->getPost("content_en"));
                    $model->setTitle_En($this->getRequest()->getPost("title_en"));
                    $result=$model->update();
                    if ($result) {
                        $id_news = $this->getRequest()->getPost("id");
                        $data_cats = $this->getRequest()->getPost('cats_item', null );
                        $model_news_cat = new Admin_Model_NewsCategory();
                        $model_news_cat->deleteAllByNewsId($id_news);
                            if(isset($data_cats)){
                                foreach ($data_cats as $item){                                                                       
                                    $model_news_cat->insert($id_news, $item);
                                }
                        }
                        $this->_redirect("/".$this->module."/news/index");
                    }
                }
            } else {
                $news = $model->getById($id);
                if ($news != null) {
               
                    $this->view->id = $news['id'];
                    $this->view->title =$news['title'];
                    $this->view->title_en = $news['title_en'];
                    $this->view->content = $news['content'];
                    
                    $this->view->content_en = $news['content_en'];
                    $this->view->des = $news['des'];
                    $this->view->des_en = $news['des_en'];
                    $this->view->action = $this->baseUrl."/".$this->module."/news/edit";
                    $this->view->method = "post";
                    $this->view->thumbnail_url = $news['thumbnail_url'];
                    $cat_model = new Admin_Model_Category();
                    $this->view->cats = $cat_model->listAll();
                    $news_cat_model = new Admin_Model_NewsCategory();                    
                    $this->view->cat_items = $news_cat_model->getByIdNews($news['id']);
                }
             
             
        
                $this->view->intro = "Update News";
                $this->view->group = "NEWS";
                $this->view->group_action = "UPDATE";
            }
        }
        public function delAction(){
            $id = $this->getRequest()->getParam('id',null);
            $model = new Admin_Model_News();
            $model->getById($id);
            $result = $model->delete();
            if($result > 0)
                   $this->_redirect("/".$this->module."/news/index");
            else 
                    $this->view->error = "Không thể xóa !!!";
             $this->view->group = "NEWS";
                $this->view->group_action = "DELETE";
        }
        
       
       

 
}
?>
